package com.tanhua.dubbo.server.api.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;

import com.tanhua.dubbo.server.api.UsersApi;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
@Slf4j
public class UserApiImpl implements UsersApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存好友关系（双向保存）
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public String saveUsers(Long userId, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }
        //查询好友关系表
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));

        long count = this.mongoTemplate.count(query, Users.class);
        if (count > 0) {
            //好友关系表存在，已经为好友关系，不需要保存
            return null;
        }
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());

        //保存用户与好友的关系
        users.setUserId(userId);
        users.setFriendId(friendId);
        this.mongoTemplate.save(users);
        //保存好友与用户的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        this.mongoTemplate.save(users);

        return users.getId().toHexString();
    }

    /**
     * 删除好友关系，双向删除
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {

        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        //删除我与好友的关系数据
        long count1 = this.mongoTemplate.remove(query1, Users.class).getDeletedCount();

        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        //删除好友与我的关系数据
        long count2 = this.mongoTemplate.remove(query2, Users.class).getDeletedCount();


        return count1 > 0 && count2 > 0;
    }

    /**
     * 根据用户id查询全部Users列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {

        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Users.class);
    }

    /**
     * 根据用户id分页查询Users列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);

        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));

        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageable);
        List<Users> userList = this.mongoTemplate.find(query, Users.class);
        pageInfo.setRecords(userList);
        return pageInfo;
    }
}
